package com.tiantianyuegou.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.tiantianyuegou.R;
import com.tiantianyuegou.model.EvaluateModel;

import java.util.ArrayList;
import java.util.List;

/**
 * 更多评价
 * Created by huyang on 2016/1/11 0011.
 */
public class EvaluateAdapter extends BaseAdapter {

    private Context context;
    private List<EvaluateModel> data;
//    private List<SpecModel> mData;
//    private SpecModel specModel ;

    public EvaluateAdapter(Context context) {
        this.context = context;
        data = new ArrayList<>();
    }

    public EvaluateAdapter(Context context, List<EvaluateModel> data) {
        this.context = context;
        this.data = data;
    }

    public void setData(List<EvaluateModel> data) {
        this.data.clear();
        this.data.addAll(data);

    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convetView, ViewGroup parent) {
        EvaluateModel model = data.get(position);

//        for (int i = 0; i <model.getData().size() ; i++) {
//            specModel=model.getData().get(i);
//        }
         ViewHolder holder;
        if (convetView == null) {
            convetView = LayoutInflater.from(context).inflate(R.layout.list_item_evaluate, null);
            holder = new ViewHolder();
            holder.iv_user = (ImageView) convetView.findViewById(R.id.iv_user);
            holder.tv_content = (TextView) convetView.findViewById(R.id.tv_content);
            holder.tv_rategrade = (TextView) convetView.findViewById(R.id.tv_rategrade);
            holder.tv_username = (TextView) convetView.findViewById(R.id.tv_username);
            holder.tv_time = (TextView) convetView.findViewById(R.id.tv_time);
            holder.tv_colortitle= (TextView) convetView.findViewById(R.id.tv_colortitle);
            holder.iv_evaluate= (ImageView) convetView.findViewById(R.id.iv_haoping);
//            holder.tv_color= (TextView) convetView.findViewById(R.id.tv_color);
//            holder.tv_sizetitle= (TextView) convetView.findViewById(R.id.tv_sizetitle);
//            holder.tv_size= (TextView) convetView.findViewById(R.id.tv_size);
            convetView.setTag(holder);
        } else {
            holder = (ViewHolder) convetView.getTag();
        }
        String tmAvater=model.getTmAvatar();
        if (model.getTmAvatar()!=null&&!"null".equals(model.getTmAvatar())){
            ImageLoader.getInstance().displayImage(model.getTmAvatar(),holder.iv_user);
        }else{
            holder.iv_user.setImageResource(R.mipmap.default_icon);
        }
        String name=model.getTmName();
        StringBuffer buffer=new StringBuffer();
        if (name.length()>=3){
            buffer.append(name.substring(0,1));
            for (int i = 0; i <2 ; i++) {
                buffer.append("*");
            }
            buffer.append(name.substring(name.length()-1));
            holder.tv_username.setText(buffer.toString());
        }else if(name.length()==2){
            buffer.append(name.substring(0,1));
                buffer.append("*");
            holder.tv_username.setText(buffer.toString());
        }else if (name.length()==1){
            holder.tv_username.setText(model.getTmName());
        }
        holder.tv_content.setText(model.getTcContent());
        holder.tv_time.setText(model.getTcAddtime());
//        for (int i = 0; i <model.getData().size() ; i++) {
//
//        }
        holder.tv_colortitle.setText(model.getSpecAll());

//        holder.tv_color.setText(specModel.getValue());
//        holder.tv_size.setText(specModel.getValue());
//        holder.tv_sizetitle.setText(specModel.getTsName());
        if ("1".equals(model.getTcRateGtade())) {
            holder.tv_rategrade.setText("差评");
            holder.iv_evaluate.setImageResource(R.mipmap.chaping);
        } else if ("2".equals(model.getTcRateGtade())) {
            holder.tv_rategrade.setText("中评");
            holder.iv_evaluate.setImageResource(R.mipmap.zhongping);
        } else if ("3".equals(model.getTcRateGtade())) {
            holder.tv_rategrade.setText("好评");
            holder.iv_evaluate.setImageResource(R.mipmap.haoping);
        }
        return convetView;
    }

    class ViewHolder {
        TextView tv_username, tv_rategrade, tv_content, tv_time, tv_colortitle, tv_color, tv_sizetitle, tv_size;
        ImageView iv_user,iv_evaluate;
    }
}
